/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function() {
        var param = $(this).val().trim();
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('name')] = param;
    });
    return searchParams;
}
//清空搜索条件
$("#Reset").click(function() {
    $('.SearchForm').val('');
});

/**
 * 页面自加载
 */
$(function() {
    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_info').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_info').datagrid({
        singleSelect: true, //允许选择多行
        striped: true,
        idField: 'id',
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/Banner/getData', //数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
            field: 'banner_img',
            title: '图标',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value,row,index){
                return "<img src='"+value+"' style='height: 40px;cursor:pointer;margin:5px 5px;'/>";
            }
        },{
            field: 'banner_introduce',
            title: '内容',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'banner_id',
            title: '操作',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value,row,index){
                var html ='';
                html +="<a onclick='deleteInfo(this);' style='color:brown;margin-left:5px' name='"+value+"' href='JavaScript:;'>删除</a>";
                html +="<a onclick='updateInfo(this);' style='color:darkslateblue;margin-left:5px' name='"+value+"' href='JavaScript:;'>修改</a>";
                return html;
            }
        }]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });
    /*搜索*/
    $('#Search').click(function () {
        $('#datatable_info').datagrid('reload');
    })

    /**
     * 添加
     */
    $(".addImg").click(function() {
        //获得修改框内容
        var content = "";
        $.ajax({
            url: 'admin/banner/addBanner',
            data: "",
            type: 'POST',
            dataType: 'HTML', //返回的数据类型
            success: function(updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    modalId: null,
                    animation: null,
                    title: '添加轮播图',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function() {
                    },
                    otherButtons: ['保存'],
                    otherButtonStyles: [],
                    bootstrapModalOption: {
                        backdrop: 'static'
                    },
                    largeSize: true,
                    clickButton: function(sender, modal, index) {
                        var formData = new FormData($( "#addForm" )[0]);
                        if($('#data_photo').val() == ''){
                            layer.msg('请上传图片',{icon:5,time:2000});
                            return;
                        }
                        if($('#myEditor').val() == ''){
                            layer.msg('内容不能为空',{icon:5,time:2000});
                            return;
                        }
                        $.ajax({
                            url: 'admin/Banner/add',
                            data: formData,
                            async: false,//1，这四个要写上，否则点击没反应
                            cache: false,//2
                            contentType: false,//3
                            processData: false,//4
                            type: 'POST',
                            dataType: 'JSON',

                            success: function(returnData) {
                                if (returnData.code == 100) {
                                    layer.msg(returnData.msg,{icon:5,time:2000});
                                } else {
                                    modal.modal('hide');
                                    layer.msg(returnData.msg,{icon:6,time:2000});
                                    $('#datatable_info').datagrid('reload');
                                }
                            },
                            error: function() {
                                layer.msg("添加失败！",{icon:5,time:2000});
                            },
                        });
                    },
                });
            },
            error: function() {
                // $.TeachDialog({
                //     content: '获取数据失败，无法进行修改',
                // });
                layer.msg("获取数据失败，无法进行修改！",{icon:5,time:2000});
                return;
            }
        });
    });


});